package com.heima.behavior.service.impl;

import com.heima.behavior.service.CollectionBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApCollectionBehavior;
import com.heima.model.behavior.pojos.ApFollowBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadUtil.AppThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CollectionBehaviorServiceImpl implements CollectionBehaviorService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 收藏行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {

        //1.非空判断
        if (dto.getEntryId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.获取登录用户
        ApUser user = AppThreadLocalUtil.getUser();
        //3.判断用户是否存在，是否游客
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }
        //4.游客无权操作
        if (user.getId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //5.设置用户收藏文章key
        String collection_key = BehaviorConstants.USER_COLLECTION_+ dto.getEntryId();

        //6.判断是否有小key
        Boolean collectionFlag = stringRedisTemplate.opsForHash().hasKey(collection_key, user.getId() + "");
        //7.判断是收藏
        //7.1并且传参是收藏状态
        if (collectionFlag && dto.getOperation() == 0) {
            return ResponseResult.okResult("收藏成功");
        }
        //7.2并且传参是取消收藏
        if (collectionFlag && dto.getOperation() == 1) {
            //7.2.1删除redis，mongodb
            stringRedisTemplate.opsForHash().delete(collection_key, user.getId() + "");
            Query query = Query.query(Criteria.where("userId").is(user.getId()).and("entryId").is(dto.getEntryId()));
            mongoTemplate.remove(query, ApCollectionBehavior.class);
            return ResponseResult.okResult("取消收藏成功");
        }
        //8.否,并且参数是收藏，新增redis，mongodb
        if (!collectionFlag && dto.getOperation() == 0) {
            stringRedisTemplate.opsForHash().put(collection_key, user.getId() + "","0");

            ApCollectionBehavior apCollectionBehavior = new ApCollectionBehavior();
            apCollectionBehavior.setId(ObjectId.get().toHexString());
            apCollectionBehavior.setUserId(Long.valueOf(user.getId()));
            apCollectionBehavior.setEntryId(dto.getEntryId());
            apCollectionBehavior.setType((short)0);
            apCollectionBehavior.setCollectionTime(new Date());

            mongoTemplate.save(apCollectionBehavior);
        }
        return ResponseResult.okResult("收藏成功");
    }

}
